<?php

namespace App\Http\Controllers\Home;

use App\Http\Controllers\Core\BaseController;
use App\Models\Category;
use App\Models\CompanyProfile;
use App\Models\District;
use App\Models\JobResumeAccess;
use App\Models\Resume;
use App\Models\ResumeFavorites;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Session;
use PhpOffice\PhpWord\TemplateProcessor;

class ResumeController extends BaseController
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        //$this->middleware('auth');
    }

    /**
     * Show the application dashboard.
     *
     * @param Request $request
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $params = $request->all();
        $category = Category::category_cache();
        $district = District::district_cache();
        //获取所有的简历信息
        $resume = Resume::searchResult($category, $params);

        $data = [
            'params' => $params,
            "resume" => $resume,
            "category" => $category
        ];
        return view('home.resume.index', $data);
    }

    public function info($id = 0)
    {
        $resume = Resume::find($id);
        if(!$resume){
            echo '您访问的内容不存在';exit;
        }
        //浏览次数+1
        $resume->click++;
        $resume->save();
        $user = Session::get('member');
        return view('home.resume.item', ['resume' => $resume, 'user' => $user]);
    }

    /**
     * @param int $id
     * @param Request $request
     * @return \Symfony\Component\HttpFoundation\BinaryFileResponse
     * @throws \PhpOffice\PhpWord\Exception\CopyFileException
     * @throws \PhpOffice\PhpWord\Exception\CreateTemporaryFileException
     */
    public function doc($id = 0, Request $request)
    {
        $member = Session::get('member');
        if (!$member || !(isSet($member->id) && $member->id)) {
          //echo "<script>alert('您还没有登陆！')</script>";
          //return false;
        }
        $resume = Resume::find($id);
        $birth_cn = date('Y') - $resume->birth;

        $templateProcessor = new TemplateProcessor('static/template/a.docx');
        $templateProcessor->setValue('nickname', $resume->nickname);
        $templateProcessor->setValue('sex_cn', ($resume->sex == 1) ? '男' : '女');
        $templateProcessor->setValue('birth_cn', $birth_cn);
        $templateProcessor->setValue('birth', $resume->birth);
        $templateProcessor->setValue('education_cn', $resume->education_cn);
        $templateProcessor->setValue('experience_cn', $resume->experience_cn);
        $templateProcessor->setValue('house', $resume->house);
        $templateProcessor->setValue('city_cn', $resume->district_cn);
        $templateProcessor->setValue('wage_cn', $resume->wage_cn);
        $templateProcessor->setValue('current_cn', $resume->current_cn);
        $templateProcessor->setValue('telephone', $resume->telephone);
        $templateProcessor->setValue('specialty', $resume->specialty);
        $templateProcessor->setValue('trade_cn', $resume->trade_cn);
        $templateProcessor->setValue('intention_cn', $resume->intention_cn);
        $templateProcessor->saveAs(storage_path('resume.docx'));
        return response()->download(storage_path('resume.docx'));

    }

    /**
     * 收藏简历
     * @param int $id
     * @return false|string
     */
    public function collect($id = 0)
    {
        $member = Session::get('member');

        if (!$member) {
            return $this->jsonResponse(1, '请登陆后再进行操作');
        }
        if ($member->type != 1) {
            return $this->jsonResponse(2, '只有企业用户可以收藏简历');
        }
        $favorites = ResumeFavorites::where(['user_id' => $member->id, 'resume_id' => $id])->get();
        if ($favorites->first()) {
            return $this->jsonResponse(3, '您已经收藏了该简历');
        }
        $company = CompanyProfile::select('id')->where(['user_id' => $member->id])->get();
        if (!$company = $company->first()) {
            return $this->jsonResponse(2, '只有企业用户可以收藏简历');
        }
        $favorites = new ResumeFavorites();
        $time = time();
        $favorites->user_id = $member->id;
        $favorites->resume_id = $id;
        $favorites->company_id = $company->id;
        $favorites->create_time = $time;
        $favorites->refresh_time = $time;
        if ($favorites->save()) {
            return $this->jsonResponse(0, '成功');
        }
        return $this->jsonResponse(4, '失败');
    }

    /**
     * 邀请面试
     * @param int $id 简历ID
     * @return false|string
     */
    public function apply($id = 0)
    {
        $member = Session::get('member');

        if (!$member) {
            return $this->jsonResponse(1, '请登陆后再进行操作');
        }
        if ($member->type != 1) {
            return $this->jsonResponse(2, '只有企业用户可以邀请面试');
        }
        return $this->jsonResponse(4, '成功');
    }


}